package com.nsl.my_youlai.bbu_return.service;

import com.nsl.my_youlai.bbu_return.enums.check.BoardMatchSlotRulesEnum;
import com.nsl.my_youlai.bbu2.enums.BoardTypeEnum;
import com.nsl.my_youlai.bbu_return.enums.GenerationTypeEnum;
import com.nsl.my_youlai.bbu_return.enums.VendorEnum;
import com.nsl.my_youlai.bbu_return.exception.EnumTransformException;
import com.nsl.my_youlai.bbu_return.model.bo.BbuBoard;
import com.nsl.my_youlai.bbu_return.model.bo.BBUFrame;
import com.nsl.my_youlai.bbu_return.model.bo.BBUSlot;
import com.nsl.my_youlai.bbu_return.model.bo.check.BbuCheckResult;
import com.nsl.my_youlai.common.base.IBaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

//todo:
// 非机框+风扇，必须有槽道号
// 厂家，网络类型和bbu型号匹配

@Slf4j
@Service
public class BBUCheckServiceImpl {

    //第一步校验: 将所有bbu frame 对应的 BbuCheckResult ，放入list集合
    public List<BbuCheckResult> checkMinimumConfiguration(List<BBUFrame> bbuFrames,List<BbuCheckResult> bbuCheckResultList){
        for (BBUFrame bbuFrame : bbuFrames) {
            Map<Integer,BBUSlot> bbuSlotMap=bbuFrame.getBbuSlotMap();
            BbuCheckResult bbuCheckResult=new BbuCheckResult();
            for (Integer slotInteger : bbuSlotMap.keySet()) {
                BBUSlot bbuSlot=bbuSlotMap.get(slotInteger);
                List<BbuBoard> bbuBoardList=bbuSlot.getBbuBoardList();
                bbuCheckResult.setNodeBId(bbuFrame.getNodeBId());
                for (BbuBoard bbuBoard : bbuBoardList) {
                    String boardType=bbuBoard.getBoardType().getLabel();
                    switch (boardType){
                        case "主控板":
                        case "交换板":
                            bbuCheckResult.setControlBoardCounter(bbuCheckResult.getControlBoardCounter()+1);
                            break;
                        case "基带板":
                            bbuCheckResult.setBaseBandBoardCounter(bbuCheckResult.getBaseBandBoardCounter()+1);
                            break;
                        case "电源板":
                            bbuCheckResult.setPowerBoardCounter(bbuCheckResult.getPowerBoardCounter()+1);
                            break;
                        case "机框+风扇":
                            bbuCheckResult.setFrameAndFunCounter(bbuCheckResult.getFrameAndFunCounter()+1);
                            break;
                        default:
                            throw new RuntimeException("nodeBid = "+bbuFrame.getNodeBId() +" 板卡类型错误。");
                    }

                }
            }


            bbuCheckResultList.add(bbuCheckResult);
        }

        for (BbuCheckResult bbuCheckResult : bbuCheckResultList) {
            List<String> minimumConfigurationList=bbuCheckResult.getMinimumConfigurationList();
            if(bbuCheckResult.getControlBoardCounter()==0){
                minimumConfigurationList.add("无主控或交换板;");
            }
            if(bbuCheckResult.getBaseBandBoardCounter()==0){
                minimumConfigurationList.add("无基带板;");
            }
            if(bbuCheckResult.getPowerBoardCounter()==0){
                minimumConfigurationList.add("无电源板;");
            }
            if(bbuCheckResult.getFrameAndFunCounter()==0){
                minimumConfigurationList.add("无机框和风扇;");
            }
        }
        return bbuCheckResultList;
    }

    public void checkBoardMatchSlotListAndSlotRepeatedUse(List<BBUFrame> bbuFrames,List<BbuCheckResult> bbuCheckResultList) throws EnumTransformException {
        for (int i=0 ;i<bbuFrames.size();i++) {
            BBUFrame bbuFrame=bbuFrames.get(i);
            BbuCheckResult bbuCheckResult = bbuCheckResultList.get(i);
            Map<Integer, BBUSlot> bbuSlotMap = bbuFrame.getBbuSlotMap();

            //String vendor=bbuFrame.getVendor();
            VendorEnum vendorEnum=bbuFrame.getVendor();
            GenerationTypeEnum generationTypeEnum=bbuFrame.getGenerationType();
            if(generationTypeEnum==null){
                log.info("bbuFrame= {}",bbuFrame);
            }
            BoardMatchSlotRulesEnum boardMatchSlotRulesEnum= IBaseEnum.getEnumByLabel(vendorEnum.getLabel()+"_"+generationTypeEnum.getLabel(), BoardMatchSlotRulesEnum.class);
            Map<BoardTypeEnum,List<Integer>> boardTypeEnumIntegerMap=boardMatchSlotRulesEnum.getBoardTypeSlotIndexMap();

            for (Map.Entry<Integer, BBUSlot> integerBBUSlotEntry : bbuSlotMap.entrySet()) {
                BBUSlot bbuSlot=integerBBUSlotEntry.getValue();
                List<BbuBoard> bbuBoardList=bbuSlot.getBbuBoardList();
                if(bbuBoardList.size()>1){
                    bbuCheckResult.getSlotRepeatedUseList().add("槽道:"+bbuSlot.getSlotIndex()+"重复占用;");
                }
                for (BbuBoard bbuBoard : bbuBoardList) {
                    BoardTypeEnum boardTypeEnum=bbuBoard.getBoardType();
                    log.info("boardTypeEnum = {}",boardTypeEnum);
                    List<Integer> slotIntegerRange=boardTypeEnumIntegerMap.get(boardTypeEnum);
                    if(!slotIntegerRange.contains(bbuSlot.getSlotIndex())){
                        bbuCheckResult.getBoardMatchSlotList().add("槽道:"+bbuSlot.getSlotIndex()+" 不能放置 "+bbuBoard.getBoardType().getLabel());
                    }
                }
            }
        }
    }
}
